package cn.itsource.basic.service.impl;

import cn.itsource.basic.exception.CustomException;
import cn.itsource.basic.service.IVerifyCodeService;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.SmsUtils;
import cn.itsource.basic.util.StrUtils;
import cn.itsource.basic.util.VerifyCodeUtils;
import cn.itsource.org.domain.User;
import cn.itsource.org.domain.dto.UserSmsCodeDto;
import cn.itsource.org.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class VerifyCodeServiceImpl implements IVerifyCodeService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;


    @Override
    public AjaxResult imgCode(String uuid) {
//        //去redis中查询该uuid的验证码是否过期
//        Object o = redisTemplate.opsForValue().get(uuid);
//        if(o != null){
//            String s = VerifyCodeUtils.VerifyCode(115, 45, o.toString());
//            return AjaxResult.me().setResultObj(s);
//        }
        //生成随机字符串
        String code = StrUtils.getRandomString(1);
        //画图并转码base64
        String s = VerifyCodeUtils.VerifyCode(115, 45, code);
        //保存redis 5分钟过期
        redisTemplate.opsForValue().set(uuid,code,5, TimeUnit.MINUTES);
        //返回图片的base64
        return AjaxResult.me().setResultObj(s);
    }

    @Override
    public AjaxResult userSmscode(UserSmsCodeDto dto) throws IOException {
//        1. 参数校验
//        2. 校验用户是否注册过
        User user = userMapper.loadByUserName(dto.getMobile());
        if(user != null){
            throw new CustomException("用户已存在!");
        }
        //去redis中通过uuid获取图形验证码
        Object o = redisTemplate.opsForValue().get(dto.getImageCodeKey());
        //查不到就是过期了
        if(o == null){
            throw new CustomException("图形验证码已过期!");
        }
        //对比和用户传的是否一致
        if(!o.toString().equalsIgnoreCase(dto.getImageCode())){
            throw new CustomException("图形验证码不正确!");
        }
        //拼接短信存redis的key
        String smsKey = dto.getMobile()+":register"; //123132123123:register
        //通过短信key从redis中获取值
        Object smsRedisValue = redisTemplate.opsForValue().get(smsKey);
        //短信验证码的公共变量
        String code;
        //如果通过短信key在redis找到了
        if(smsRedisValue != null){
            //1234:13216541651651
            String redisValue = smsRedisValue.toString();
            //从查询结果中取出时间戳
            Long sendTime = Long.valueOf(redisValue.split(":")[1]);
            //从查询结果中取出验证码
            code = redisValue.split(":")[0];
            //通过当前时间戳减去发送时候的时间戳来判断是否可以重发
            if(System.currentTimeMillis() - sendTime < 60000){
                throw new CustomException("请勿频繁发送!!!稍后再试！！！");
            }
        }else{
            code = StrUtils.getRandomString(1);
        }
        //存redis 发送验证码
        redisTemplate.opsForValue().set(smsKey,code+":"+System.currentTimeMillis(),3,TimeUnit.MINUTES);
//        if(SmsUtils.sendSms(dto.getMobile(),code)){
//            return AjaxResult.me();
//        }else{
//            return AjaxResult.me().setSuccess(false);
//        }
        log.info(">>>>>>>>>>>>>>>>>>>>>本次的验证码为:{}",code);
        return AjaxResult.me();
    }

}
